package me.jzn.alib.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ArrayRes;
import androidx.annotation.BoolRes;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntegerRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.RawRes;
import androidx.annotation.StringRes;
import androidx.annotation.XmlRes;
import androidx.core.content.ContextCompat;

import java.io.InputStream;

import me.jzn.alib.ALib;
import me.jzn.core.utils.IoUtil;
import me.jzn.core.utils.StrUtil;

/**
 * @author xqs @date 2019/7/12
 */
public class ResUtil {
    //IncorrectContextUseViolation
//    private static LayoutInflater sInflater = LayoutInflater.from(ALib.app());
    private static Resources sResources = ALib.app().getResources();

    //@see  inflate(context,int)
//    @Deprecated
//    public static <T extends View> T inflate(@LayoutRes int layoutId) {
//        return (T) sInflater.inflate(layoutId, null);
//    }

    public static <T extends View> T inflate(Context context, @LayoutRes int layoutId) {
        return (T) LayoutInflater.from(context).inflate(layoutId, null);
    }

    public static <T extends View> T inflate(@LayoutRes int layoutId, ViewGroup parent) {
        LayoutInflater inflate = LayoutInflater.from(parent.getContext());
        return (T) inflate.inflate(layoutId, parent, false);
    }

    public static <T extends View> T inflateAndAdd(@LayoutRes int layoutId, ViewGroup parent) {
        LayoutInflater inflate = LayoutInflater.from(parent.getContext());
        return (T) inflate.inflate(layoutId, parent, true);
    }

    //@see  inflate(context,int)
    @Deprecated
    public static <T extends View> T inflateAndAdd(Context context, @LayoutRes int layoutId, ViewGroup parent) {
        return (T) LayoutInflater.from(context).inflate(layoutId, parent, true);
    }

    //@see  inflate(int,parent)
    @Deprecated
    public static <T extends View> T inflate(Context context, @LayoutRes int layoutId, ViewGroup parent) {
        return (T) LayoutInflater.from(context).inflate(layoutId, parent, false);
    }

    public static String getString(@StringRes int res) {
        return ALib.app().getString(res);
    }

    public static int getInt(@IntegerRes int res) {
        return sResources.getInteger(res);
    }

    public static int[] getIntArray(@ArrayRes int res) {
        return sResources.getIntArray(res);
    }

    public static boolean getBoolean(@BoolRes int res) {
        return sResources.getBoolean(res);
    }

    public static String getString(@StringRes int resId, Object... formatArgs) {
        return ALib.app().getString(resId, formatArgs);
    }

    public static String[] getStringArray(@ArrayRes int res) {
        return sResources.getStringArray(res);
    }

    public static Drawable getDrawable(@DrawableRes int resId) {
        return ContextCompat.getDrawable(ALib.app(), resId);
    }

    public static int getColor(@ColorRes int resId) {
        return ContextCompat.getColor(ALib.app(), resId);
    }

    public static int getDimen(@DimenRes int resId) {
        return sResources.getDimensionPixelSize(resId);
    }

    public static TypedArray obtainTypedArray(@ArrayRes int arrayRes) {
        return sResources.obtainTypedArray(arrayRes);
    }

    public static String readUtf8FromRaw(@RawRes int rawid) {
        InputStream is = getRaw(rawid);
        byte[] bytes = IoUtil.readAll(is);
        return StrUtil.newUtf8(bytes);
    }

    public static InputStream getRaw(@RawRes int id) {
        return sResources.openRawResource(id);
    }

    public static XmlResourceParser getXml(@XmlRes int id) {
        return sResources.getXml(id);
    }

    public static Resources getResources() {
        return sResources;
    }

    public static AssetManager getAssets() {
        return ALib.app().getAssets();
    }
}
